/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.toegepaste.www;

import info.toegepaste.www.beans.Account;
import info.toegepaste.www.beans.Alumnus;
import info.toegepaste.www.beans.Bedrijf;
import info.toegepaste.www.beans.Functie;
import info.toegepaste.www.beans.Inschrijving;
import info.toegepaste.www.beans.Richting;
import info.toegepaste.www.beans.Werkstatus;
import info.toegepaste.www.dataaccess.DAAccount;
import info.toegepaste.www.dataaccess.DAAlumnus;
import info.toegepaste.www.dataaccess.DABedrijf;
import info.toegepaste.www.dataaccess.DAEvent;
import info.toegepaste.www.dataaccess.DAFunctie;
import info.toegepaste.www.dataaccess.DAInschrijving;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Logic
 */
@WebServlet(name = "AdminServlet", urlPatterns = {"/AdminServlet"}, initParams = {
    @WebInitParam(name = "url", value = "jdbc:mysql://localhost/lesson"),
    @WebInitParam(name = "login", value = "dba"),
    @WebInitParam(name = "password", value = "sql")})
public class AdminServlet extends HttpServlet {

    DAAccount daaccount = null;
    DAAlumnus daalumnus = null;
    DABedrijf dabedrijf = null;
    DAFunctie dafunctie = null;
    DAInschrijving dainschrijving = null;
    DAEvent daevent = null;
    
    @Override
    public void destroy() {
        try {
            if (daaccount != null) {
                daaccount.close();
            }
            if (daalumnus != null) {
                daalumnus.close();
            }
            if (dafunctie != null) {
                dafunctie.close();
            }
            if (dabedrijf != null) {
                dabedrijf.close();
            }
            if (dainschrijving != null) {
                dainschrijving.close();
            }
            if (daevent != null) {
                daevent.close();
            }

        } catch (SQLException e) {
        }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    public void init() throws ServletException {
        try {
            String url = getInitParameter("url");
            String password = getInitParameter("password");
            String login = getInitParameter("login");


            if (daaccount == null) {
                daaccount = new DAAccount(url, login, password);
            }
            if (daalumnus == null) {
                daalumnus = new DAAlumnus(url, login, password);
            }
            if (dafunctie == null) {
                dafunctie = new DAFunctie(url, login, password);
            }
            if (dabedrijf == null) {
                dabedrijf = new DABedrijf(url, login, password);
            }
            if (dainschrijving == null) {
                dainschrijving = new DAInschrijving(url, login, password);
            }
            if (daevent == null) {
                daevent = new DAEvent(url, login, password);
            }


        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd = null;
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=UTF-8");

        if (request.getParameter("statistiekButton") != null) {
            try {
                request.setAttribute("aantalAlumni", daalumnus.countAll());
                request.setAttribute("werkendeAlumni", daalumnus.countWerkendeAlumni());
                request.setAttribute("werklozeAlumni", daalumnus.countWerklozeAlumni());
                request.setAttribute("top3bedrijven", dabedrijf.getTop3Bedrijven());
                request.setAttribute("applicatieAlumni", daalumnus.countApplicatieAlumni());
                request.setAttribute("businessAlumni", daalumnus.countBusinessAlumni());
                request.setAttribute("netwerkAlumni", daalumnus.countNetwerkAlumni());
            } catch (SQLException ex) {
                Logger.getLogger(AdminServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            rd = request.getRequestDispatcher("statistieken.jsp");
        }

        if (request.getParameter("criteria") != null) {
        }
        if (request.getParameter("eventtoevoegen") != null) {
            rd = request.getRequestDispatcher("eventtoevoegen.jsp");
        }
        if (request.getParameter("addevent") != null) {
            java.util.Date utildate1, utildate2;
            java.sql.Date sqldate1 = null, sqldate2 = null;
            //Datum "sinds" alumnus
            java.util.Date utilDate;
            try {
                utilDate = new SimpleDateFormat("dd/MM/yyyy").parse(request.getParameter("datumevent"));
                java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                
            } catch (ParseException ex) {
                Logger.getLogger(RegistratieServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            try {
                utildate1 = new SimpleDateFormat("yyyy/MM/dd").parse(request.getParameter("datumevent"));
                sqldate1 = new java.sql.Date(utildate1.getTime());
            } catch (ParseException ex) {
                Logger.getLogger(AdminServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            try {
                utildate2 = new SimpleDateFormat("yyyy/MM/dd").parse(request.getParameter("eindeinschrijvingdatum"));
                sqldate2 = new java.sql.Date (utildate2.getTime());
            } catch (ParseException ex) {
                Logger.getLogger(AdminServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            daevent.insertEvent(request.getParameter("eventnaam"),request.getParameter("eventbeschrijving"), request.getParameter("plaats"), sqldate1, sqldate2);
        }
        if (request.getParameter("wijzigen") != null) {
            Alumnus nieuweAlumnus = new Alumnus();
            String test = request.getParameter("achternaam");
            nieuweAlumnus.setAlumnusID(Integer.parseInt(request.getParameter("alumnusid")));
            nieuweAlumnus.setVoornaam(request.getParameter("voornaam"));
            nieuweAlumnus.setAchternaam(request.getParameter("achternaam"));
            nieuweAlumnus.setEmailprive(request.getParameter("emailprive"));
            nieuweAlumnus.setFunctie(new Functie(Integer.parseInt(request.getParameter("functie"))));
            nieuweAlumnus.setBedrijf(new Bedrijf(Integer.parseInt(request.getParameter("bedrijf"))));
            nieuweAlumnus.setWerkstatus(new Werkstatus(Integer.parseInt(request.getParameter("werkstatus"))));
            nieuweAlumnus.setRichting(new Richting(Integer.parseInt(request.getParameter("richting"))));
            nieuweAlumnus.setAfstudeerjaar(request.getParameter("afstudeerjaar"));
            java.util.Date utilDate;
            try {
                utilDate = new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("sinds"));
                java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                nieuweAlumnus.setSinds(sqlDate);
            } catch (ParseException ex) {
                Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                daalumnus.update(nieuweAlumnus);
            } catch (SQLException ex) {
                Logger.getLogger(AdminServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (session != null) {
                session.invalidate();
            }
            Account login;
            login = daaccount.checkCredentials(request.getParameter("username"), request.getParameter("password"));
            if (login.getAccountID() != 0) {
                try {

                    Alumnus alumnus = daalumnus.getAlumnus(login.getAlumnusID());
                    session.setAttribute("alumnus", alumnus);

                } catch (SQLException sQLException) {
                }


                session.setAttribute("login", login);
            } else {
                rd = request.getRequestDispatcher("errorPage.jsp");
                rd.forward(request, response);
            }
            rd = request.getRequestDispatcher("index.jsp");
            String bedankt = "Uw gegevens zijn succesvol aangepast!";
            request.setAttribute("bedankt", bedankt);
            rd = request.getRequestDispatcher("bedankt.jsp");
        }
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
